﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DotNetCore.BannerManagement.Domain;
using Microsoft.Practices.Repository;

using System.Globalization;

namespace DotNetCore.BannerManagement.DAL.Domain.dnc_AdvertisementRepositoryArtifacts
{
    /// <summary>
    /// This factory class takes a dnc_Advertisement object and generates
    /// the dbcommand object needed to call the underlying update
    /// stored proc.
    /// </summary>
    internal class dnc_AdvertisementUpdateFactory : IDbToBusinessEntityNameMapper, IUpdateFactory<dnc_Advertisement>
    {
        /// <summary>
        /// Creates the dnc_AdvertisementUpdateFactory to build an update statement for
        /// the given dnc_Advertisement object.
        /// </summary>
        /// <param name="dnc_Advertisement">dnc_Advertisement to update into the database.</param>
        public dnc_AdvertisementUpdateFactory()
        {
        }
        
        #region IUpdateFactory<dnc_Advertisement> Members

        public DbCommand ConstructUpdateCommand(Database db, dnc_Advertisement dnc_Advertisement)
        {
			DbCommand command = db.GetStoredProcCommand("dbo.Updatednc_Advertisement");
			
			db.AddInParameter(command, "active", DbType.Boolean, dnc_Advertisement.Active);
            db.AddInParameter(command, "imageID", DbType.Int32, dnc_Advertisement.ImageID);
			db.AddInParameter(command, "advertiserID", DbType.Int32, dnc_Advertisement.AdvertiserID);
			if(dnc_Advertisement.AlternateText != null)
			{
				db.AddInParameter(command, "alternateText", DbType.String, dnc_Advertisement.AlternateText);
			}
			if(dnc_Advertisement.EndDate != null)
			{
				db.AddInParameter(command, "endDate", DbType.DateTime, dnc_Advertisement.EndDate);
			}
			db.AddInParameter(command, "iD", DbType.Int32, dnc_Advertisement.ID);
			if(dnc_Advertisement.Name != null)
			{
				db.AddInParameter(command, "name", DbType.String, dnc_Advertisement.Name);
			}
			if(dnc_Advertisement.startDate != null)
			{
				db.AddInParameter(command, "startDate", DbType.DateTime, dnc_Advertisement.startDate);
			}
			if(dnc_Advertisement.URL != null)
			{
				db.AddInParameter(command, "uRL", DbType.String, dnc_Advertisement.URL);
			}
			return command;
        }
        #endregion
        
		#region IDbToBusinessEntityNameMapper Members
        /// <summary>
        /// Maps a field name in the database (usually a parameter name for a stored proc)
        /// to the corresponding business entity property name.
        /// </summary>
        /// <remarks>This method is intended for error message handling, not for reflection.</remarks>
        /// <param name="dbParameter">Name of field/parameter that the database knows about.</param>
        /// <returns>Corresponding business entity field name.</returns>
        public string MapDbParameterToBusinessEntityProperty(string dbParameter)
        {
            switch (dbParameter)
            {
				case "active":
					return "Active";
				case "advertiserID":
					return "AdvertiserID";
				case "alternateText":
					return "AlternateText";
				case "endDate":
					return "EndDate";
				case "iD":
					return "ID";
				case "name":
					return "Name";
				case "startDate":
					return "startDate";
				case "uRL":
					return "URL";
				default:
					throw new Exception(dbParameter);
			}
        }
        #endregion        
    }
}

